<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">

      <el-form-item label="模块名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模块名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="模块标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入模块标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="教材" prop="bookId">
        <el-select v-model="queryParams.bookId" placeholder="请选择教材" clearable size="small" filterable>
          <el-option
            v-for="dict in dictData('sys_entity_book')"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['knowledge:module:add']"
        >新增</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['knowledge:module:edit']"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['knowledge:module:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['knowledge:module:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="info"
                plain
                icon="el-icon-files"
                size="mini"
                @click="handleTrash"
                v-hasPermi="['knowledge:module:trash']"
        >回收站</el-button>
      </el-col>


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" ref="listTable" :data="moduleList" @selection-change="handleSelectionChange" @sort-change="handleSortChange" :height="tableHeight">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="模块名称" align="center" prop="name"  />
      <el-table-column label="模块标题" align="center" prop="title"  />
      <el-table-column label="教材" align="center" prop="bookId" >
        <template slot-scope="scope">
          <dict-tag :options="dictData('sys_entity_book')" :value="scope.row.bookId"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
	        <el-button
		        class="text-info"
		        size="mini"
		        type="text"
		        icon="el-icon-info"
		        @click="handleInfo(scope.row)"
		        v-hasPermi="['knowledge:module:info']"
	        >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['knowledge:module:edit']"
          >修改</el-button>
          <el-button
            class="text-danger"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['knowledge:module:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模块对话框 -->
    <module-form v-if="form" :item.sync="form" @success="onSubmitSuccess" />
	
	  <!-- 模块详情 对话框 -->
	  <module-info v-if="info" :item.sync="info" @modify="handleUpdate" />

    <!-- 回收站 -->
	<module-trash v-if="trash" :show.sync="trash" @success="onSubmitSuccess" />

  </div>
</template>

<script>
import * as moduleService from '@/api/service/knowledge/ModuleService'
import ModuleForm from './form'
import ModuleInfo from './info'
import ModuleTrash from './trash'
export default {
  name: "Module",
  components:{
    ModuleTrash,
    ModuleForm,
      ModuleInfo
  },
	dictionaries: ['sys_entity_book'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],

      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模块表格数据
      moduleList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        title: null,
        bookId: null,
      },
      // 表单参数
      form: null,
      // 详情
      info: null,
      // 回收站
      trash: null

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模块列表 */
    getList() {
      this.loading = true;

      // 默认排序设置
      //if(!this.queryParams.orderByColumn){
      //  this.queryParams.orderByColumn = 'createTime';
      //  this.queryParams.isAsc = 'descending';
      //}
      moduleService.findList(this.queryParams).then(resp => {
        this.moduleList = resp.items;
        this.total = resp.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.form = {}
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      this.form = {id: id}
    },
	  /** 查看按钮操作 */
    handleInfo(row){
		  const id = row.id || this.ids
		  this.info = {id: id}
    },
	  /** 回收站 */
    handleTrash(){
      this.trash = true;
    },

    /** 提交成功 */
    onSubmitSuccess(form){
        this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除模块编号为"' + ids + '"的数据项?').then(function() {
          return moduleService.remove(ids);
        }).then(() => {
          this.getList();
          this.$modal.success("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有模块数据项?').then(() => {
          this.exportLoading = true;
          return moduleService.exportData(queryParams);
        }).then(resp => {
          this.$downloader.download(resp).finally(() => {
            setTimeout(() => {
              this.exportLoading = false;
            }, 1000)
          });

        }).catch(() => {
          this.exportLoading = false;
          this.$modal.error("导出失败, 请稍后重试");
        });
    }
  }
};
</script>
